import sys
import time
import pytest

from pygments.lexers import data
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from DButils import *
from common1.account_readdata import get_data
from common1.login_operation import *
from common1.account_operation import *

class Test_account:

    @pytest.fixture(scope="function", autouse=True)
    def setup_teardown(self):
        service = Service("chromedriver.exe")
        self.driver = webdriver.Chrome(service=service)
        self.driver.get("http://localhost:90")
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        yield
        self.driver.quit()

    # 正常修改原密码测试
    @pytest.mark.parametrize("username, password, y_password, x_password, q_password, expect", get_data(0))
    def test_login(self, username, password, y_password, x_password, q_password, expect):
        login_normal(self.driver, username, password)
        account(self.driver, y_password, x_password, q_password)

        sql = "select * from user where username = %s and password = %s"
        param = [username,"fcea920f7412b5da7be0cf42b8c93759"]
        expect = len(select(sql, param))
        assert expect == 1

        sql = "update user SET password = %s where username = %s"
        param = ["e10adc3949ba59abbe56e057f20f883e", "lisi"]
        update(sql, param)

    #原密码错误或为特殊字符
    @pytest.mark.parametrize("username, password, y_password, x_password, q_password, expect", get_data(1))
    def test_error(self, username, password, y_password, x_password, q_password, expect):
        login_normal(self.driver, username, password)
        account(self.driver, y_password, x_password, q_password)

        result = self.driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/span").text
        assert expect == result

        sql = "select * from user where username = %s and password = %s"
        param = ["lisi", "e10adc3949ba59abbe56e057f20f883e"]
        expect = len(select(sql, param))
        assert expect == 1

    #原密码为空修改密码测试
    @pytest.mark.parametrize("username, password, y_password, x_password, q_password, expect", get_data(2))
    def test_none(self, username, password, y_password, x_password, q_password, expect):
        login_normal(self.driver, username, password)
        account(self.driver, y_password, x_password, q_password)

        result = self.driver.find_element(By.XPATH,
                                          "/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/span").text
        assert expect == result

        sql = "select * from user where username = %s and password = %s"
        param = ["lisi", "e10adc3949ba59abbe56e057f20f883e"]
        expect = len(select(sql, param))
        assert expect == 1

    #确认密码特殊字符测试
    @pytest.mark.parametrize("username, password, y_password, x_password, q_password, expect", get_data(3))
    def test_none(self, username, password, y_password, x_password, q_password, expect):
        login_normal(self.driver, username, password)
        account(self.driver, y_password, x_password, q_password)

        sql = "select * from user where username = %s and password = %s"
        param = ["lisi", "d98035d303c2468bca3fe004c57d7441"]
        expect = len(select(sql, param))
        assert expect == 1

        sql = "update user SET password = %s where username = %s"
        param = ["e10adc3949ba59abbe56e057f20f883e", "lisi"]
        update(sql, param)

    #确认密码为空测试测试
    @pytest.mark.parametrize("username, password, y_password, x_password, q_password, expect", get_data(4))
    def test_q(self, username, password, y_password, x_password, q_password, expect):
        login_normal(self.driver, username, password)
        account(self.driver, y_password, x_password, q_password)

        result = self.driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[2]/span").text
        assert expect == result

        sql = "select * from user where username = %s and password = %s"
        param = ["lisi", "e10adc3949ba59abbe56e057f20f883e"]
        expect = len(select(sql, param))
        assert expect == 1

    #新密码和确认密码不同测试
    @pytest.mark.parametrize("username, password, y_password, x_password, q_password, expect", get_data(5))
    def test_different(self, username, password, y_password, x_password, q_password, expect):
        login_normal(self.driver, username, password)
        account(self.driver, y_password, x_password, q_password)

        result = self.driver.find_element(By.XPATH,
                                          "/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/span").text
        assert expect == result

        sql = "select * from user where username = %s and password = %s"
        param = ["lisi", "e10adc3949ba59abbe56e057f20f883e"]
        expect = len(select(sql, param))
        assert expect == 1


















